#!/bin/sh

#
# Test streaming a resource through an arbitrary command on the
# target system
#

. "$(cd "$(dirname "$0")" && pwd)/common.sh"

OUTFILE=$WORK/output1k

if [ "$CC" = "x86_64-w64-mingw32-gcc" -o "$MODE" = "windows" ]; then
    # This test needs a Windows executable that reads from STDIN.
    # No suitable executables are currently on Travis CI Image
    exit 77
fi

cat >"$CONFIG" <<EOF
file-resource TEST {
	host-path = "${TESTFILE_1K}"
}
task complete {
	on-resource TEST { pipe_write("dd of=${OUTFILE}") }
}
task bad_command {
	on-resource TEST { pipe_write("badcommand1234") }
}
task failing_command {
	on-resource TEST { pipe_write("false") }
}
task short_command {
	on-resource TEST { pipe_write("dd of=${OUTFILE} count=1") }
}
EOF

# Create the firmware file like normal
$FWUP_CREATE -c -f "$CONFIG" -o "$FWFILE"

# Check that pipe_write fails w/o --unsafe
if $FWUP_APPLY -a -d "$IMGFILE" -i "$FWFILE" -t complete; then
    echo "No --unsafe:  The pipe_write should have failed."
    exit 1
fi

# Pipe the contents of the firmware file through fwup
rm -f "$OUTFILE"
$FWUP_APPLY --unsafe -a -d "$IMGFILE" -i "$FWFILE" -t complete
cmp "$TESTFILE_1K" "$OUTFILE"

# Try it again, but use /dev/null since that's a thing people do with pipe_write
# (This whole test doesn't run on Windows, so Windows check for /dev/null isn't necessary)
rm -f "$OUTFILE"
$FWUP_APPLY_NO_CHECK --unsafe -a -d /dev/null -i "$FWFILE" -t complete
cmp "$TESTFILE_1K" "$OUTFILE"

# Try applying the update to a bogus command (should fail)
if $FWUP_APPLY --unsafe -a -d "$IMGFILE" -i "$FWFILE" -t bad_command; then
    echo "Expected pipe_write to fail when invoking an unknown command."
    exit 1
fi

# Try applying the update to a failing command (should fail)
if $FWUP_APPLY --unsafe -a -d "$IMGFILE" -i "$FWFILE" -t failing_command; then
    echo "Expected pipe_write to fail when the command failed."
    exit 1
fi

# Try applying the update to a command that doesn't accept all input (should fail)
#if $FWUP_APPLY --unsafe -a -d $IMGFILE -i $FWFILE -t short_command; then
#    echo "Expected pipe_write to fail when the command doesn't take all input."
#    exit 1
#fi
